import pymysql
import random
from common import snowFaker as sf
from python_faker.faker11 import getDishes


def get_random_dishes():
    # 连接数据库
    conn = pymysql.connect(host="localhost", user="root", password="123456", database="esl", charset="utf8mb4")
    cursor = conn.cursor()

    dishes = getDishes()
    for dish in dishes:
        query = f"SELECT specification_id FROM specification where canteen_id = '{dish[1]}'"
        cursor.execute(query)
        conn.commit()
        # 获取数据
        specifications = cursor.fetchall()
        specification = random.sample(list(specifications), 2)
        for s in specification:
            query = """insert into dish_specification(id,dish_id,specification_id) 
                        VALUES (%s, %s, %s)"""
            id = sf.get_id()
            data =(id,dish[0],s)
            cursor.execute(query,data)
            conn.commit()
            print("数据插入成功！")

    # 关闭连接
    cursor.close()
    conn.close()

if __name__ == "__main__":
    get_random_dishes()
